/**
 * rainbow.com Inc.
 * Copyright (c) 2014-2015 All Rights Reserved.
 */
package com.rainbow.bam;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rainbow.bam.entity.HtAccount;
import com.rainbow.bam.entity.HtAccountDuty;
import com.rainbow.bam.service.account.HtAccountService;
import com.rainbow.bam.service.resource.HtSourceService;

/**
 * 基础接口服务类
 * 
 * @author HarrisonHan
 * @version $Id: BaseServiceImpl.java, v 0.1 2015年11月20日 上午10:31:07 HarrisonHan Exp $
 */
@Service
public class BaseServiceImpl implements BaseService {

    /**
     *  账户接口服务
     */
    @Autowired
    private HtAccountService htAccountService;
    /**
     * 资源接口服务
     */
    @Autowired
    private HtSourceService  htResourceService;

    /** 
     * @see com.rainbow.bam.BaseService#findRoles(java.lang.String)
     */
    @Override
    public Set<String> findRoles(String accountName) {
        HtAccount user = htAccountService.findByAccountName(accountName);
        Set<String> roles = new HashSet<String>();
        if (user != null) {
            Iterator<HtAccountDuty> it = user.getHtAccountDuties().iterator();
            HtAccountDuty htAccountDuty = null;
            while (it.hasNext()) {
                htAccountDuty = it.next();
                roles.add(htAccountDuty.getHtDuty().getName());
            }
        }
        return roles;
    }

    /** 
     * @see com.rainbow.bam.BaseService#findPermissions(java.lang.String)
     */
    @Override
    public Set<String> findPermissions(String accountName) {
        HtAccount user = htAccountService.findByAccountName(accountName);
        if (user == null) {
            return Collections.emptySet();
        }
        return this.htResourceService.initAccountDefaultAuthority(user);
    }

}
